package com.ez.supervise.action.biz;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.ez.supervise.action.base.BaseJsonAction;
import com.ez.supervise.core.Page;
import com.ez.supervise.core.Result;
import com.ez.supervise.model.biz.DemocracyGoal;
import com.ez.supervise.model.biz.DemocracyScore;
import com.ez.supervise.model.biz.OrgDemocracy;
import com.ez.supervise.model.dto.LoginUser;
import com.ez.supervise.model.sys.Org;
import com.ez.supervise.service.DemocracyGoalService;
import com.ez.supervise.service.DemocracyScoreService;
import com.ez.supervise.service.OrgDemocracyService;
import com.ez.supervise.service.OrgService;
import com.ez.supervise.util.JsonUtils;

public class OrgDemocracyAction extends BaseJsonAction {

    private static final long serialVersionUID = -5237037468195860016L;
    private Long id;
    private OrgDemocracyService orgDemocracyService;
    private DemocracyGoalService democracyGoalService;
    private DemocracyScoreService democracyScoreService;

    private OrgDemocracy orgDemocracy;
    private OrgService orgService;

    private Page page;
    private Integer year;
    private String orgType;
    private List<Org> orgList;
    private List<DemocracyGoal> democracyGoalList;
    private static List<DemocracyScore> democracyScoreList;
    private String data;

    public String saveOrgDemocracy() {
	try {
	    if (StringUtils.isNotBlank(data)) {
		int count = orgDemocracyService.countOrgDemocracy(year, getLoginUser().getUserId());
		if(count > 0) {
		    Result result = new Result(true, "你已经社会评价过了，不能再次评价!");
		    outJsonString(JsonUtils.fromObject(result, new String[] { "entity"}));
		    return SUCCESS;
		}
		List<OrgDemocracy> dataList = makeDemocracy(data, year, getLoginUser());
		orgDemocracyService.saveOrgDemocracy(dataList);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }


    public String findAllOrgDemocracy() {
	try {
	    page = new Page();
	    List result = orgDemocracyService.findAll();
	    page.setRoot(result);
	    page.setTotalProperty(result.size());
	    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
		"isDeleted", "new" }));

	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    public String findAll() {
	page = new Page();
	page.setTotalProperty(0);
	page.setRoot(orgDemocracyService.findAll());

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new", });

	JSONObject jsonObj = JSONObject.fromObject(page, config);
	// System.out.println(jsonObj.toString());
	outJsonString(jsonObj.toString());
	return SUCCESS;
    }

    /**
     * @return
     */
    public String checkDemocracy() {
	try {
	    String userId = getRequest().getParameter("userId");
	    String year = getRequest().getParameter("year");
	    if (StringUtils.isNotBlank(userId) && StringUtils.isNotBlank(year) && NumberUtils.isNumber(userId) &&
		    NumberUtils.isNumber(year)) {
		int count = orgDemocracyService.countOrgDemocracy(Integer.valueOf(year), Long.valueOf(userId));
		if (count == 0) {
		    outJsonString(RESULT_SUCCESS);
		    return SUCCESS;
		}
	    }
	    outJsonString(RESULT_FAIL);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    /**
     * @return
     * @throws Exception
     */
    public String updateOrgDemocracy() throws Exception {
	String fieldName = getRequest().getParameter("fieldName");
	String fieldValue = getRequest().getParameter("fieldValue");
	String strOrgDemocracyId = getRequest().getParameter("id");
	try {
	    if (id != null && id > 0) {
		OrgDemocracy saveOrgDemocracy = new OrgDemocracy();
		saveOrgDemocracy.setId(id);
		BeanUtils.setProperty(saveOrgDemocracy, fieldName, fieldValue);
		orgDemocracyService.update(saveOrgDemocracy);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}

	return SUCCESS;
    }

    public String findDemocracyDetail() throws Exception {
	orgList = orgService.findOrgByType(orgType);
	if (year == null) {
	    year = Calendar.getInstance().get(Calendar.YEAR);
	}
	democracyGoalList = democracyGoalService.findDemocracyGoal(year, orgType);
	return SUCCESS;
    }

    public void setYear(Integer year) {
	this.year = year;
    }

    public Long getId() {
	return id;
    }

    public void setId(Long id) {
	this.id = id;
    }

    public void setOrgType(String orgType) {
	this.orgType = orgType;
    }

    public String getOrgType() {
	return orgType;
    }

    public void setOrgDemocracyService(OrgDemocracyService orgDemocracyService) {
	this.orgDemocracyService = orgDemocracyService;
    }

    public void setOrgService(OrgService orgService) {
	this.orgService = orgService;
    }

    public void setDemocracyGoalService(DemocracyGoalService democracyGoalService) {
	this.democracyGoalService = democracyGoalService;
    }

    public void setDemocracyScoreService(DemocracyScoreService democracyScoreService) {
	this.democracyScoreService = democracyScoreService;
    }

    public OrgDemocracy getOrgDemocracy() {
	return orgDemocracy;
    }

    public void setOrgDemocracy(OrgDemocracy orgDemocracy) {
	this.orgDemocracy = orgDemocracy;
    }

    public Page getPage() {
	return page;
    }

    public void setPage(Page page) {
	this.page = page;
    }

    public void setOrgList(List<Org> orgList) {
	this.orgList = orgList;
    }

    public List<Org> getOrgList() {
	return orgList;
    }

    public List<DemocracyScore> getDemocracyScoreList() {
	if (democracyScoreList == null) {
	    democracyScoreList = democracyScoreService.findAll();
	}
	return democracyScoreList;
    }

    public static void clearDemocracyScoreList() {
	if (democracyScoreList != null) {
	    democracyScoreList.clear();
	    democracyScoreList = null;
	}
    }

    public void setDemocracyGoalList(List<DemocracyGoal> democracyGoalList) {
	this.democracyGoalList = democracyGoalList;
    }

    public List<DemocracyGoal> getDemocracyGoalList() {
	return democracyGoalList;
    }

    public void setData(String data) {
	this.data = data;
    }

    public String getData() {
	return data;
    }

    public static void main(String[] args) {
	
    }

    public final static List<OrgDemocracy> makeDemocracy(String data,Integer year, LoginUser loginUser) {
	String temp  = "";
	String[] tempArray;
	List< OrgDemocracy> dataList = new ArrayList<OrgDemocracy>();
	OrgDemocracy orgDemocracy = null;
	String[] arrayData = data.split(",");
	if(ArrayUtils.isEmpty(arrayData)){
	    return dataList;
	}
	for (int i = 0; i < arrayData.length; i++) {
	    temp = arrayData[i];
	    tempArray = temp.split("=");
	    temp = tempArray[0];
	    orgDemocracy = new OrgDemocracy();
	    orgDemocracy.setScoreId(Long.valueOf(tempArray[1]));
	    tempArray = temp.split("_"); 
	    orgDemocracy.setOrgId(Long.valueOf(tempArray[2]));
	    orgDemocracy.setGoalId(Long.valueOf(tempArray[3]));
	    orgDemocracy.setYear(year);
	    orgDemocracy.setCheckManId(loginUser.getDemocracyId());
	    orgDemocracy.setUserId(loginUser.getUserId());
	    dataList.add(orgDemocracy);
	}
	return dataList;
    }

}
